<?php
namespace app\service;
use think\facade\Db;
use app\model\MonthlyModel;
use Exception;

class MonthlyService{  
    public function list(){
        $data = MonthlyModel::order('id','desc')->select(); 
        $count = MonthlyModel::count(); 
        return ["list"=>$data,"count"=>$count];
    }  

    public function save($params){              
        if(empty($params['id'])){
            $data = MonthlyModel::where('year_month',$params['year_month'])->find();
            if($data){
                throw new Exception("日期已存在");
            }
            $data = MonthlyModel::create($params); 
        }else{
            $data = MonthlyModel::update($params, ['id' => $params['id']]); 
        }        
        return $data;
    }

    public function delete($params){   
        $data = MonthlyModel::destroy($params['id']);         
        return $data;
    }
}